package com.originskyseed.platform.cms.controller;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.cms.i18n.CmsResponseMsg;
import com.originskyseed.platform.cms.model.Gift;
import com.originskyseed.platform.cms.service.IGiftService;
import com.originskyseed.platform.cms.vo.request.GiftRequestVO;
import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.user.model.UserInfo;

/**
 * 礼品信息Controller类（OSI_GIFT）
 *
 * @author WuZhuoXi
 */
@Controller
@RequestMapping("/gift")
@BaseLocaleResource(localeFileName = "i18n_cms")
public class GiftController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(GiftController.class);

	@DubboReference
	private IGiftService giftService;

	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/giftList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<Gift>> giftList(HttpServletRequest request) {
		BaseResponseBody<BasePage<Gift>> result = null;
		try {
			// 请求对象
			GiftRequestVO requestVO = this.getParameterBean(GiftRequestVO.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			// 分页查询
			BasePage<Gift> page = new BasePage<Gift>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.giftService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<Gift>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<Gift>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/giftSave.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<Gift> giftSave(HttpServletRequest request) {
		BaseResponseBody<Gift> result = null;
		try {
			// 请求对象
			Gift gift = this.getParameterBean(Gift.class);

			if (StringUtils.isNotBlank(gift.getId())) {
				this.giftService.updateByPrimaryKeySelective(gift);
			} else {
				UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
				gift.setCreatorId(userInfo.getId());
				gift.setCreator(userInfo.getName());
				gift.setStatus(0);
				gift.setIsDel(0);
				this.giftService.insertSelective(gift);
			}
			result = new BaseResponseBody<Gift>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.GIFT_SAVE_SUCCESS.getMsg()), gift);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Gift>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.GIFT_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/giftDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> giftDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});
			this.giftService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTAUDIT_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.CONTENTAUDIT_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 假删除信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/giftDeleteById.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> giftDeleteById(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});
			this.giftService.updateIsDeleteByIds(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 详情
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/giftDetails.json", method = RequestMethod.POST)
	@RequiredParams(names = { "id" })
	public BaseResponseBody<Gift> giftDetails(HttpServletRequest request) {
		BaseResponseBody<Gift> result = null;
		try {
			// 请求参数
			String id = this.getParameterString("id", null);

			Gift gift = this.giftService.selectByPrimaryKey(id);
			result = new BaseResponseBody<Gift>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_SUCCESS.getMsg()), gift);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Gift>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CmsResponseMsg.DATA_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}
}
